package com.zzz.servlet;

import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

/*
 * @author zzr
 * @date: 2025/07/05  21:58
 * @description:
 */
public class servlet4 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        // 获取 ServletContext
        // 法 1：
        //ServletContext servletContext = servletConfig.getServletContext();
        // 法 2：
        //ServletContext servletContext = req.getServletContext();
        // 法 3：
        ServletContext servletContext = getServletContext();
        String encoding = servletContext.getInitParameter("encoding");
        System.out.println("encoding  = " + encoding);

        Enumeration<String> initParameterNames = servletContext.getInitParameterNames();
        while (initParameterNames.hasMoreElements()) {
            String pname = initParameterNames.nextElement();
            System.out.println(pname + " = " + servletContext.getInitParameter(pname));
        }
    }
}
